
# include <stdio.h>

void print3Dmatrix(int*** D3_num, int n, int m, int l) //以***D3_num形式将D3_num的首地址传入
{
    int i, ii, iii;
    printf("\n3D matrix is: \n");
    for (i = 0; i < n; i++) {
        printf("[]\n", i);
        for (ii = 0; ii < m; ii++) {

            for (iii = 0; iii < l; iii++) {
                printf("%.3d ", D3_num[i][ii][iii]);
            }
            printf("\n");
        }
    }
}

int** D3ToD2(int*** D3_num) //int **表示返回的是一个二级指针
{
    int** D2_num;
    D2_num = D3_num[0];//D3_num是一个三级指针，D3num[0]是一个二级指针，D3num[0][0]是一个一级指针
    return D2_num;
}

void print2Dmatrix(int** D2_num, int m, int l)
{
    int ii, iii;
    printf("\n2D matrix is:\n");
    for (ii = 0; ii < m; ii++) {

        for (iii = 0; iii < l; iii++) {
            printf("%.3d ", D2_num[ii][iii]);
        }
        printf("\n");
    }

}

int main()
{
    int i, ii, iii;//初始化参数
    int n = 2, m = 3, l = 4; //三维数组长度

    int*** D3_num;    //声明一个三级指针
    D3_num = (int***)malloc(n * sizeof(int**));    //分配空间，该三级指针指向n个二级指针
    for (i = 0; i < n; i++) {
        D3_num[i] = (int**)malloc(m * sizeof(int*));//分配空间，每个二级指针又指向m个一级指针
        for (ii = 0; ii < m; ii++) {
            D3_num[i][ii] = (int*)malloc(l * sizeof(int));//分配空间，每个一级指针指向长度为l的一维数组
            for (iii = 0; iii < l; iii++) {
                D3_num[i][ii][iii] = (i + 1) * (ii + 2) * (iii + 3);//为每个数组赋值，这里D3_num[i][ii][iii]的形式与我们平常用三维数组的形式一致
                //但实际D3_num[i][ii][iii]是一种简写，C编译器解释起来是 *(*(*(p+i)+ii)+iii),所以可以直接赋值
            }
        }
    }

    print3Dmatrix(D3_num, n, m, l);//输出D3_num内容，这里直接将D3_num作为三级指针输入

    int** D2_num; //三级指针可以表示三维数组，二级指针自然就表示二维数组了:D
    D2_num = D3ToD2(D3_num);//D3ToD2返回一个二级指针，与D2_num匹配

    print2Dmatrix(D2_num, m, l);
    getchar();
    return 0;
}




	/// 9 3 3
	int ** *p= (int***)malloc(sizeof(int **) * 9);

	for (int i=0;i<9;i++)
	{
		p[i] = (int**)malloc(sizeof(int *)*3);
		for (int j=0;j<3;j++)
		{
			p[i][j] = (int *)malloc(sizeof(int)*3);
			for (int k=0;k<3;k++)
			{
				p[i][j][k] = i + j + k;
			}
		}
	}